package jp.co.dailyReport.customer;

import org.apache.log4j.Logger;
import java.sql.SQLException;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.customer.Customer;
import jp.co.dailyReport.customer.sql.CustomerDeleteExecuter;
import jp.co.dailyReport.customer.sql.CustomerInsertExecuter;
import jp.co.dailyReport.customer.sql.CustomerLoadExecuter;
import jp.co.dailyReport.customer.sql.CustomersLoadExecuter;
import jp.co.dailyReport.customer.sql.CustomerUpdateExecuter;
import jp.co.dailyReport.PermissionManager;
import jp.co.dailyReport.PermissionException;

/**
 * 取引先情報管理クラス
 */
public class CustomerManager {

	private static Logger log = Logger.getLogger(CustomerManager.class);

	/**
	 * 取引先情報を取得します。
	 * 
	 * @return 取引先オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static Customer[] load() throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		CustomersLoadExecuter customersLoadExecuter = new CustomersLoadExecuter();
		Object objs[] = (Object[])null;

		try {
			objs = ExecuterManager.execute(connectionFactory, customersLoadExecuter);
		} catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs == null || objs.length == 0) {
			return new Customer[0];
		} else {
			return (Customer[])objs;
		}
	}

	/**
	 * 社員の権限で取引先情報を取得します。
	 * 
	 * @param staff 社員オブジェト
	 * @return 取引先オブジェクトの配列
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static Customer[] load(Staff staff) throws DataUnavailableException ,PermissionException{

		Customer[] customers = null;
		//取引先情報登録権限の判定
		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_REGISTRATION,PermissionManager.METHOD_READ)){
			if(customers==null) customers = load();
		}
		//取引先情報更新権限の判定
		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_UPDATE,PermissionManager.METHOD_READ)){
			if(customers==null) customers = load();
		}
		//取引先情報削除権限の判定
		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_DELETE,PermissionManager.METHOD_READ)){
			if(customers==null) customers = load();
		}
		if(customers==null){
			throw new PermissionException("Permission denied to all functions customer");
		}
		return customers;
	}

	/**
	 * 取引先情報を取得します。
	 * 
	 * @param customerId　取引先ID
	 * @return 取引先オブジェクト
	 * @throws DataUnavailableException
	 */
	public static Customer load(int customerId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		CustomerLoadExecuter customerLoadExecuter = new CustomerLoadExecuter(customerId);
		Object objs[] = (Object[])null;
		try {
			objs = ExecuterManager.execute(connectionFactory, customerLoadExecuter);
        } catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs == null || objs.length == 0) {
			return new Customer();
		} else {
			return (Customer)objs[0];
		}
	}

	/**
	 * 取引先情報を更新します。
	 * 
	 * @param customer 取引先オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void update(Customer customer) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		CustomerUpdateExecuter customerUpdateExecuter = new CustomerUpdateExecuter(customer);
		try {
			ExecuterManager.execute(connectionFactory, customerUpdateExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 社員の権限で取引先情報を更新します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param targetCustomer 取引先オブジェクト
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void update(Staff staff,Customer targetCustomer) throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_UPDATE,PermissionManager.METHOD_WRITE)) {
			update(targetCustomer);
		} else {
			throw new PermissionException("Permission denied to update customer");
		}
	}

	/**
	 * 取引先情報を登録します。
	 * 
	 * @param customer 取引先オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void insert(Customer customer) throws DataUnavailableException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        CustomerInsertExecuter customerInsertExecuter = new CustomerInsertExecuter(customer);
		try {
            ExecuterManager.execute(connectionFactory, customerInsertExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 社員の権限で取引先情報を登録します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param targetCustomer 取引先オブジェクト
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void insert(Staff staff,Customer targetCustomer) throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_REGISTRATION,PermissionManager.METHOD_WRITE)){
			insert(targetCustomer);
		}else{
			throw new PermissionException("Permission denied to insert customer");
		}
	}

	/**
	 * 取引先情報を削除します。
	 * 
	 * @param customerId　取引先ID
	 * @throws DataUnavailableException
	 */
	public static void delete(int customerId) throws DataUnavailableException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        CustomerDeleteExecuter customerDeleteExecuter = new CustomerDeleteExecuter(customerId);
		try {
            ExecuterManager.execute(connectionFactory, customerDeleteExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 社員の権限で取引先情報を削除します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param targetCustomerId 取引先ID
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void delete(Staff staff ,int targetCustomerId) throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,PermissionManager.CUSTOMER_DELETE,PermissionManager.METHOD_WRITE)){
			delete(targetCustomerId);
		} else {
			throw new PermissionException(" Permission denied to delete customer");
		}
	}

	/**
	 * 取引先情報の存在チェックを行います。
	 * 
	 * @param customerId　取引先ID
	 * @return true→存在する、false→存在しない
	 * @throws DataUnavailableException
	 */
	public static boolean isExist(int customerId) throws DataUnavailableException {

		boolean flg = false;
        ConnectionFactory connectionFactory = new ConnectionFactory();
        CustomerLoadExecuter customerLoadExecuter = new CustomerLoadExecuter(customerId);
        Object objs[] = (Object[])null;

		try {
			objs = ExecuterManager.execute(connectionFactory, customerLoadExecuter);
		} catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if(objs != null && objs.length != 0){
			Customer customer = (Customer)objs[0];
			flg = !customer.isDelFlg();
		}
		return flg;
	}
}